package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.enumerate.InputMode;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * 设置输入模式
 */

/**
 * 1.配置异步信号优先的时候
 * 无论是否接入同步信号，inputModeActive == dvi
 * 2.配置同步信号优先的时候
 * 当有接入同步信号，inputModeActive == hdmi
 * 当没有接入同步信号，inputModeActive == dvi
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetInputMode implements CompleteEntityScheduleActions {
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class InputMsg implements Serializable {
		/**
		 * 要设置的输入模式
		 * hdmi - 优先显示HDMI信号
		 * dvi - 优先显示设备内容
		 * 信号源序号，从0开始,显示的序号加1，如0对应的HDMI1
		 */
		private InputMode inputMode;
		private Integer sourceIndex;
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	@Builder
	public static class OutputMsg implements Serializable {
		/**
		 * 当前输入信号模式
		 * 当前信号源模式序号，从0开始,显示的序号加1，如0对应的HDMI1
		 * 当前输入信号源序号，从0开始,显示的序号加1，如0对应的HDMI1
		 */
		private InputMode inputMode;
		
		/**
		 * 当前输入信号
		 */
		private InputMode inputModeActive;
		private Integer sourceIndex;
		private Integer sourceIndexActive;
		
	}
	
	private InputMsg input;
	private OutputMsg output;
	
	@Override
	public ByteString objectConvertBytes() {
		final CompleteInstruction.SetInputMode.InputMsg.Builder inputMsg =
				CompleteInstruction.SetInputMode.InputMsg.newBuilder();
		SetInputMode.InputMsg thisInput = this.getInput();
		inputMsg.setInputMode(thisInput.getInputMode().toProto());
        inputMsg.setSourceIndex(thisInput.getSourceIndex());
		final CompleteInstruction.SetInputMode.Builder builder =
				CompleteInstruction.SetInputMode.newBuilder();
		builder.setInput(inputMsg);
		return builder.build().toByteString();
	}
	
	@Override
	public SetInputMode byteConvertObject(ByteString bytes) {
		CompleteInstruction.SetInputMode setInputMode = null;
		try {
			setInputMode = CompleteInstruction.SetInputMode.parseFrom(bytes);
		} catch (InvalidProtocolBufferException e) {
			log.error("", e);
		}
		if (Objects.isNull(setInputMode)) {
			throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
		}
		SetInputMode.OutputMsg outputMsg = new SetInputMode.OutputMsg();
		CompleteInstruction.SetInputMode.OutputMsg output = setInputMode.getOutput();
		outputMsg.setInputMode(InputMode.getInputModeStruct(output.getInputMode()));
		outputMsg.setInputModeActive(InputMode.getInputModeStruct(output.getInputModeActive()));
        outputMsg.setSourceIndex(output.getSourceIndex());
        outputMsg.setSourceIndexActive(output.getSourceIndexActive());
        this.setOutput(outputMsg);
		return this;
	}
}

